package com.zhongkai.controller;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.app.service.SumService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Chinaset;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 财务资产
 * */
@Controller
public class SumController {

    @Autowired
    private SumService sumService;



    /**
     * 获取公司资产情况
     * */
    @RequestMapping(value = "/pro_con/get_sum", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult supplier_add() {


        ZhongkaiResult result = sumService.getAsset();

        return result;
    }



    /**
     * 修改金额
     *
     * @param sum  金额
     * @param asset_elite  摘要信息
     * */
    @RequestMapping(value = "/pro_con/updata_ca", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult updataSum(String sum, String asset_elite,HttpServletRequest httpServletRequest) {

        Chinaset chinaset = (Chinaset) httpServletRequest.getAttribute("user");
        System.out.println(chinaset.getSetName());
        System.out.println(asset_elite);
        System.out.println(sum);
        ZhongkaiResult result = sumService.updataSum(sum,chinaset.getSetName() , asset_elite);

        return result;
    }


    /**
     *新增应收款项
     *
     * @param asset_name 款项名称
     * @param asset_elite 摘要信息
     * @param asset_sum 涉及到的金额
     * @param asset_time 预计回款时间
     * @param name 操作人员
     *
     */

    /**
     *新增应付款项
     *
     * @param asset_name 款项名称
     * @param asset_elite 摘要信息
     * @param asset_sum 涉及到的金额
     * @param asset_time 预计付款时间
     *
     */
    @RequestMapping(value = "/pro_con/add_mps", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult addMinusPaySum(String asset_name, String asset_elite, String asset_sum, String asset_time, String type,HttpServletRequest httpServletRequest) {

        if (StringUtils.isNullOrEmpty(asset_time)) {
            return ZhongkaiResult.build(400,"数据不完整");
        }
        DateTime dateTime = new DateTime(asset_time);
        Chinaset chinaset = (Chinaset) httpServletRequest.getAttribute("user");

        ZhongkaiResult result;
        if ("0".equals(type)) { //0为应收
            result = sumService.addMinusSum(asset_name, asset_elite, asset_sum, dateTime.getMillis(), chinaset.getSetName());

        } else { //1为应付
            result = sumService.addPayOut(asset_name, asset_elite, asset_sum, dateTime.getMillis(), chinaset.getSetName());
        }

        return result;
    }




    /**
     * 获取公司资产情况
     * */
    @RequestMapping(value = "/pro_con/get_asset", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult getAsset() {


        ZhongkaiResult result = sumService.getAsset();

        return result;
    }


    /**
     * 获取公司资产情况
     * */
    @RequestMapping(value = "/pro_con/get_operation", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult getOperationDate(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer rows) {


        ZhongkaiResult result = sumService.getOperationDate(page,rows);

        return result;
    }




}
